﻿<?
Class Gimage{ 
var $input_type = ""; //input image format 
var $output_type = "jpg"; //output image format 
var $out_w = 0; //
var $out_h = 0; //
var $filename = ""; //filename 
var $jpeg_quality = 90; //jpeg quality 
var $save_file = ''; //save filename 
var $wm_text = ""; //
var $wm_transparence = 40; //
var $wm_size = 30; // 
var $wm_angle = 0; //
var $wm_x = 30; //
var $wm_y = 30; //
var $wm_color = array("#ff0000","#00ff00","#0000ff","#ffff00","#ff00ff","#00ffff","#000000"); //
var $wm_fontfile = "tahoma.ttf";//

function create($filename="") 
{ 
if ($filename) $this->filename = $filename; 
if(!file_exists($filename)){// 

header("Content-type: image/jpeg");
$dst_img=imagecreatetruecolor(120,100);
$wm_color = imagecolorallocatealpha($dst_img, 255, 200, 100, 0); 
imagettftext($dst_img, 12, 0, 10, 50, $wm_color, $this->wm_fontfile,"File is  \nnot exists!"); 
$src_img=ImageJPEG($dst_img, "", 0); 
exit;
}
  

if (!$this->input_type) $this->get_type(); 
if (!$this->output_type) $this->output_type = $this->input_type; 
  
if ($this->input_type == "jpg") $this->input_type = "jpeg"; 
if ($this->output_type == "jpg") $this->output_type = "jpeg"; 
  
switch ($this->input_type){ 
 case 'gif': 
  $src_img=ImageCreateFromGIF($this->filename); 
  break; 

 case 'jpeg': 
  $src_img=ImageCreateFromJPEG($this->filename); 
  break; 

 case 'png': 
  $src_img=ImageCreateFromPNG($this->filename); 
  break; 

 default: 
  $src_img=ImageCreateFromString($this->filename); 
  break; 

} 
  
  
$src_w=ImageSX($src_img); 
$src_h=ImageSY($src_img); 
if($this->out_w!=0){
$this->out_h=round($this->out_w*$src_h/$src_w);
}
if($this->out_h!=0){
$this->out_w=round($this->out_h*$src_w/$src_h);
}
if ($this->out_w!=0 && $this->out_h!=0)
{
 $dst_img=imagecreatetruecolor($this->out_w,$this->out_h); 
 imagecopyresampled($dst_img,$src_img,0,0,0,0,$this->out_w,$this->out_h,$src_w,$src_h); 

  $new_h=$this->out_h;
  $new_w=$this->out_w;

}else{ 
 //$dst_img = $src_img; 
  $new_h=$src_h;
  $new_w=$src_w;
 $dst_img=imagecreatetruecolor($new_w,$new_h); 
 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img)); 
} 
imagedestroy($src_img); 







if ($this->wm_text) 
{ 
//$this->wm_x=$src_w-strlen($this->wm_text)*$this->wm_size-10;
$this->wm_y=$new_h-4;
 $wm_color = imagecolorallocatealpha($dst_img, 255, 255, 255, $this->wm_transparence); 
 $wm_color1 = imagecolorallocatealpha($dst_img, 0, 0, 0, $this->wm_transparence); 
imagettftext($dst_img, $this->wm_size, $this->wm_angle, $this->wm_x+1, $this->wm_y+1, $wm_color1, $this->wm_fontfile, $this->wm_text);
imagettftext($dst_img, $this->wm_size, $this->wm_angle, $this->wm_x, $this->wm_y, $wm_color, $this->wm_fontfile, $this->wm_text); 

} 

if ($this->save_file) 
{ 
 switch ($this->output_type){ 
  case 'gif': 
  $src_img=ImagePNG($dst_img, $this->save_file); 
  break; 

  case 'jpeg': 
  $src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality); 
  break; 

  case 'png': 
  $src_img=ImagePNG($dst_img, $this->save_file); 
  break; 

  default: 
  $src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality); 
  break; 

 }  
} 
else 
{ 
 header("Content-type: image/{$this->output_type}"); 
 switch ($this->output_type){ 
  case 'gif': 
  $src_img=ImagePNG($dst_img); 
  break; 

  case 'jpeg': 
  $src_img=ImageJPEG($dst_img, "", $this->jpeg_quality); 
  break; 

  case 'png': 
  $src_img=ImagePNG($dst_img); 
  break; 

  default: 
  $src_img=ImageJPEG($dst_img, "", $this->jpeg_quality); 
  break; 

 } 
} 
  

imagedestroy($dst_img); 
  
} 
function get_type()//
{ 
$name_array = explode(".",$this->filename); 
if (preg_match("/\.(jpg|jpeg|gif|png)$/", $this->filename, $matches)) 
{ 
 $this->input_type = strtolower($matches[1]); 
} 
else 
{ 
 $this->input_type = "string"; 
} 

} 

} 
?>